/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.CargoDao;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dusin.util.DateUtil;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class CargoDaoImpl implements CargoDao{
    
    static Logger logger = Logger.getLogger(CargoDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public CargoDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public List<Map<String, Object>> findAll(int start, int size) {
        String sql = "select * from vcargo limit ?,?";
        return jdbcTemplate.queryForList(sql,start,start+size);
    }
    
     @Override
    public int findCount(String sql ,Object[] args){
        return jdbcTemplate.queryForInt(sql, args);
    }
    
    @Override
    public List<Map<String,Object>> findAll(String sql ,Object[] args){
        logger.info(sql+"?"+Arrays.asList(args));
        return jdbcTemplate.queryForList(sql, args);
    }

    //创建记录,提交基本数据记录号recordNo,车牌号plateNo,驻厂员号siterID,工厂编码factoryCode,
    //时间arrivalTime,status,初始状态0
    @Override
    public boolean doCreate(String recordNo,String plateNo,int siterID,String factoryCode,
            String arrivalTime,String rid){
        boolean flag = true;
        String sql = null;
        sql = "insert into acargo(recordNo,plateNo,siterID,factoryCode,arrivalTime,status,tripRid) values(?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, recordNo,plateNo,siterID,factoryCode,arrivalTime,0,rid);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
     @Override
    public boolean doCreate(String  recordNo,String plateNo,int siterID,String factoryCode,
            String arrivalTime,String grossWeight,String tareWeight,String netWeight,String minusImpurity,
            String companyCode,String productCode){
        boolean flag = true;
        String sql = null;
        sql = "insert into acargo(recordNo,plateNo,siterID,factoryCode,arrivalTime,grossWeight,tareWeight,netWeight,minusImpurity,companyCode,productCode,status) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, recordNo,plateNo,siterID,factoryCode,arrivalTime,grossWeight,tareWeight,netWeight,minusImpurity,companyCode,productCode,0);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    //根据type，更新不同照片的文件名
    @Override
    public boolean doUpdatePhotoNameByRecordNo(String recordNo, String type, String filename) {
        boolean flag = true;
        String sql = null;
        sql = "update acargo set "+type+"=?  where recordNo=?";
        int rows = jdbcTemplate.update(sql, filename,recordNo);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }  
     
    
    //填写榜单数据,毛重,皮重，扣杂，扣水，供应商，废品种类,状态表示数据已填写最低位修改成1（状态在服务端多余），
    //应为字段填了就表示有，没填表示无
    @Override
    public boolean doUpdatePaperDataByRecordNo(String recordNo,double grossWeight,double tareWeight,double minusImpurity,
            double minusWater,double netWeight,int unit,String productCode,String companyCode){
            boolean flag = true;
        String sql = null;
        sql = "update acargo set grossWeight=?,tareWeight=?,minusImpurity=?,"
                + "minusWater=?, netWeight=?,unit=?,productCode=?,companyCode=? ,status=status|1 where recordNo=?";
        int rows = jdbcTemplate.update(sql, grossWeight,tareWeight,minusImpurity,minusWater,
                netWeight,unit,productCode,companyCode,recordNo);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from acargo where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateData(int ID,String recordNo, String plateNo, int siterID, String factoryCode, 
            String arrivalTime, String grossWeight, String tareWeight, String netWeight, String minusImpurity, 
            String companyCode, String productCode) {
        boolean flag = true;
         String sql = null;
        sql = "update acargo set recordNo=?, plateNo=?,siterID=?,factoryCode=?,"
                + "arrivalTime=?, grossWeight=?,tareWeight=?,netWeight=?,minusImpurity=?,"
                + "companyCode=? ,productCode=?,status=status|1 where ID=?";
        int rows = jdbcTemplate.update(sql, recordNo,plateNo,siterID,factoryCode,arrivalTime,
                grossWeight,tareWeight,netWeight,minusImpurity,companyCode,productCode,ID);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateAllPhotoByRecordNo(String recordNo, String frontFilename, String sideFilename, String licenseFilename, String paperFilename) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean doUpdateBitById(int ID, int bits) {
        boolean flag = true;
         String sql = null;
        sql = "update acargo set status=status|? where ID=?";
        int rows = jdbcTemplate.update(sql, bits,ID);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public boolean doUpdateBitByRecNo(String recNo, int bits) {
        boolean flag = true;
         String sql = null;
        sql = "update acargo set status=status|? where recordNo=?";
        int rows = jdbcTemplate.update(sql, bits,recNo);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }   
    
     @Override
    public List<Map<String, Object>> findPathByRID(String rid) {
        String sql = null;
        try{
            sql = "select * from  location where tripRid = ?";
            return jdbcTemplate.queryForList(sql,rid);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }

    @Override
    public List<Map<String, Object>> findAllRunningTrucks(String factoryCode) {
        List<Object> argList = new ArrayList<Object>(); 
        
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -2);
        String dateStr = DateUtil.get(cal.getTime(), "yyyy-MM-dd");
        argList.add(dateStr);
        
        String fcClause=null;
        if(factoryCode!=null){
            fcClause="and fCode=?";
            argList.add(factoryCode);
        }else{
            fcClause = "";
        }        
        String sql = "select * from vtrip where startTime>? and (status=1) "+fcClause;
        logger.info(sql+"?"+argList);
        return jdbcTemplate.queryForList(sql,argList.toArray());
    }
    
    
}
